package org.rednebula.framework.core;

import java.io.Serializable;

import org.rednebula.framework.core.exception.LandException;





public class BasicConfiguration implements IConfiguration, Serializable {


	protected transient Object applicationContext = null;

	protected transient ProcedureEngine procedureEngine;
	
	protected String configurationimpl;
	
	protected String environmentimpl;


	public BasicConfiguration() {
		super();
	}
	
	
	public String getEnvironmentimpl() {
		return environmentimpl;
	}

	public void setEnvironmentimpl(String environmentimpl) {
		this.environmentimpl = environmentimpl;
	}

	public String getConfigurationimpl() {
		return configurationimpl;
	}

	public void setConfigurationimpl(String configurationimpl) {
		this.configurationimpl = configurationimpl;
	}

	public ProcedureEngine getProcedureEngine() {
		return procedureEngine;
	}

	public void setProcedureEngine(ProcedureEngine procedureEngine) {
		this.procedureEngine = procedureEngine;
	}

	public Object getApplicationContext() {
		return applicationContext;
	}

	public void setApplicationContext(Object applicationContext) {
		this.applicationContext = applicationContext;
	}

	public ProcedureEngine buildProcessEngine() {
		procedureEngine = new ProcedureEngine();
		procedureEngine.setConfiguration(this);
		return procedureEngine;
	}

	public  Class<?> findClass(String classname) {
		try {
			Class<?> implementationClass = null;
			try {
				implementationClass = Class.forName(classname, true,
						getClassLoader());
			} catch (ClassNotFoundException ex) {
				implementationClass = Class.forName(classname);
			}
			return implementationClass;
		} catch (Exception e) {
			throw new LandException("findClass " + classname, e);
		}
	}

	protected ClassLoader getClassLoader() {
		return Thread.currentThread().getContextClassLoader();
	}

	public Class getEnvironmentClass() {
		if(getEnvironmentimpl()!=null){
			return findClass(this.getEnvironmentimpl());
		}
		return BasicEnvironment.class;
	}
	
	public Class getConfigurationClass(){
		if(this.getConfigurationimpl()!=null){
			return findClass(this.getConfigurationimpl());
		}
		return BasicConfiguration.class;
	}
	
	public BasicConfiguration getConfigurationImpl(){
		try{
			return (BasicConfiguration)getConfigurationClass().newInstance();
		}catch(Exception g){
			return this;
		}
	}
	
	public  IEnvironment getEnvironmentImpl(){
		
		try{
			return (IEnvironment)getEnvironmentClass().newInstance();
		}catch(Exception g){
			throw new LandException("error: getEnvironmentImpl(). ");
		}
	}
}
